home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / ms-0_06.lha / mslaved-0.06 / mslaved.c < prev    next >
C/C++ Source or Header  |  1991-10-14  |  8KB  |  261 lines

  1. /* mslaved.c - MandelSpawn computation server deamon */
  2.  
  3. /*  This file is part of MandelSpawn, a parallel Mandelbrot program for
  4.     the X window system.
  5.  
  6.     Copyright (C) 1990 Andreas Gustafsson
  7.  
  8.     MandelSpawn is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License, version 1,
  10.     as published by the Free Software Foundation.
  11.  
  12.     MandelSpawn is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License,
  18.     version 1, along with this program; if not, write to the Free 
  19.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include <signal.h>
  28. #include <syslog.h>
  29.  
  30. #include "ms_real.h"
  31. #include "ms_ipc.h"
  32. #include "ms_job.h"
  33. #include "ms_real.c"
  34.  
  35. /* Note that the timeout default below is overridden by mslavedc so that */
  36. /* manually started servers will persist throughout a typical session. */
  37. #define DEFAULT_SLAVE_TIMEOUT (60)   /* by default time out in 60 seconds */
  38. #define DEFAULT_NICE (10)         /* use nice 10 by default */
  39.  
  40. char *me;                 /* name of program */
  41. int timeout=DEFAULT_SLAVE_TIMEOUT;    /* timeout */
  42. int niceval=DEFAULT_NICE;         /* nice value */
  43.  
  44. /* Log an error message and exit */
  45. error(s)
  46.      char *s;
  47. { syslog(LOG_ERR, "%s: %s (%m)", me, s);
  48.   exit(1);
  49. }
  50.  
  51.  
  52. /* Do the Mandelbrot calculation; return the size of the output message */
  53. /* (in bytes) */
  54.  
  55. unsigned int calculate(in, out)
  56.      Message *in, *out;
  57. { register real x_re, x_im;
  58.   register real c_re, c_im;
  59.   register real xresq, ximsq;
  60.   int julia;         /* true if calculating a Julia set */
  61.   int show_interior;     /* true if displaying interior structure */
  62.   complex corner;
  63.   complex delta;
  64.   complex z0;
  65.   
  66.   int xc, yc, xmin, xmax, xsize, ymin, ymax, ysize;
  67.   unsigned int maxiter, count;
  68.   unsigned long mi_count=0;
  69.   unsigned int datasize;
  70.  
  71.   ms_job *job;
  72.   job=(ms_job *)in->whip.data;
  73.   
  74.   /* check that the format is supported */
  75.   if(ntohs(in->whip.header.format) != DATA_FORMAT)
  76.     return(0);
  77.  
  78.   /* convert values in the message to host byte order and precalculate some */
  79.   /* useful values */
  80.   xmin=ntohs(job->s.x);
  81.   xsize=ntohs(job->s.width);
  82.   xmax=xmin+xsize;
  83.   ymin=ntohs(job->s.y);
  84.   ysize=ntohs(job->s.height);
  85.   ymax=ymin+ysize;
  86.  
  87.   show_interior = !!(ntohs(job->j.flags) & MS_OPT_INTERIOR);
  88.   julia = ntohs(job->j.julia);
  89.   corner.re = net_to_real(ntohli(job->j.corner.re));
  90.   corner.im = net_to_real(ntohli(job->j.corner.im));
  91.   z0.re = net_to_real(ntohli(job->j.z0.re));
  92.   z0.im = net_to_real(ntohli(job->j.z0.im));
  93.   delta.re = net_to_real(ntohli(job->j.delta.re));
  94.   delta.im = net_to_real(ntohli(job->j.delta.im));
  95.   
  96.   maxiter = ntohli(job->j.iteration_limit);
  97.  
  98.   datasize = (maxiter > 256) ?
  99.     ((char *)(&(out->reply.data.shorts[xsize*ysize]))
  100.      -(char *)(&(out->reply))) :
  101.        ((char *)(&(out->reply.data.chars[xsize*ysize]))
  102.     -(char *)(&(out->reply)));
  103.   
  104.   /* Perform a simple sanity check to avoid getting into semi-infinite */
  105.   /* loops because of malicious or erroneous messages.  */
  106.   if(datasize > MAX_DATAGRAM) 
  107.     error("data too large");
  108.  
  109.   /* Make sure the iteration counts can be represented in the result */
  110.   /* packet; 65536 iterations can still take a long time and if we */
  111.   /* keep calculating the same packet for too long the timeout alarm will */
  112.   /* get us.  Those who do lots of iterations should use smaller chunks. */
  113.   if(maxiter >= 65536)
  114.     error("iteration count too large");
  115.   
  116.   for(yc=ymin; yc<ymax; yc++)
  117.   { real var_re, var_im;
  118.     real old_re=zero_real(), old_im=zero_real();
  119.     var_im=add_real(julia ? z0.im : corner.im, mul_real_int(delta.im, yc));
  120.     for(xc=xmin; xc<xmax; xc++)
  121.     { var_re=add_real(julia ? z0.re : corner.re, mul_real_int(delta.re, xc));
  122.       if(julia)
  123.       {    c_re=corner.re; c_im=corner.im;
  124.     x_re=var_re; x_im=var_im;
  125.       }
  126.       else /* Mandelbrot */
  127.       {    c_re=var_re; c_im=var_im;
  128.     x_re=z0.re; x_im=z0.im;
  129.       }
  130.       /* The following loop is where the Real Work gets done. */
  131.       count=0;
  132.       while(count < maxiter-1)
  133.       {    /* The following if statement implements limit cycle detection */
  134.     /* to speed up calculation of areas inside the Mandelbrot set. */
  135.     if((count & (count-1)) == 0)
  136.     { /* "count" is zero or a power of two; save the current position */
  137.       old_re=x_re;
  138.       old_im=x_im;
  139.     }
  140.     else
  141.     { /* check if we have returned to a previously saved position; */
  142.       /* if so, the iteration has converged to a limit cycle => we */
  143.       /* are inside the Mandlebrot set and need iterate no further */
  144.       if(x_re==old_re && x_im==old_im)
  145.       { if(! show_interior)
  146.           count = maxiter-1;
  147.         break;
  148.       }
  149.     }
  150.     /* this is the familiar "z := z^2 + c; abort if |z| > 2" */
  151.     /* Mandelbrot iteration, with the arithmetic operators hidden */
  152.     /* in macros so that the same code can be compiled for either */
  153.         /* fixed-point or floating-point arithmetic. */
  154.     /* The macros (mul_real(), etc.) are defined in ms_real.h. */
  155.     xresq=mul_real(x_re, x_re);
  156.     ximsq=mul_real(x_im, x_im);
  157.     if(gteq_real(add_real(xresq, ximsq), four_real()))
  158.       break;
  159.     x_im=add_real(twice_mul_real(x_re, x_im), c_im);
  160.     x_re=add_real(sub_real(xresq, ximsq), c_re);
  161.     count++;
  162.       }
  163.       if(maxiter > 256)
  164.     out->reply.data.shorts[(yc-ymin)*xsize+(xc-xmin)]=htons(count);
  165.       else
  166.     out->reply.data.chars[(yc-ymin)*xsize+(xc-xmin)]=count;
  167.       mi_count+=count;
  168.     }
  169.   }
  170.   out->reply.mi_count=htonl(mi_count);
  171.   return(datasize);
  172. }
  173.  
  174.  
  175. void serve()
  176. { Message in;
  177.   Message out;
  178.   int isock;
  179.   
  180.   struct sockaddr_in oname;
  181.   isock=0; /* use standard input */
  182.  
  183.   while(1)
  184.   { unsigned int bytes;
  185.     int onamelen=sizeof(oname);
  186.     int version;
  187.     if(timeout != 0)
  188.       alarm(timeout);
  189.     /* receive from anywhere, save the address of the caller in oname */
  190.     if(recvfrom(isock, (char *)&in, sizeof(in), 0,
  191.         (struct sockaddr *)&oname, &onamelen) < 0) 
  192.       error("receiving datagram packet");
  193.     version=ntohs(in.generic.header.version);
  194.     if(ntohs(in.generic.header.magic)==MAGIC && version==VERSION)
  195.     { switch(ntohs(in.generic.header.type))
  196.       {    case WHIP_MESSAGE:
  197.       /* copy the header and id structures as such while still in */
  198.       /* network byte order (not strictly portable but probably works) */
  199.       out.reply.header=in.whip.header;
  200.       out.reply.id=in.whip.id;
  201.       /* just the message type needs to be changed */
  202.       out.reply.header.type=htons(REPLY_MESSAGE);
  203.       /* calculate() sets the data and mi_count fields */
  204.       bytes=calculate(&in, &out);
  205.       if(bytes)
  206.         if(sendto(isock, (char *)&out, (int)bytes, 0,
  207.               (struct sockaddr *)&oname, sizeof(oname)) < 0)
  208.           error("sending calculated data");
  209.       break;
  210.     case WHO_R_U_MESSAGE:
  211.       oname.sin_port=in.who.port; /* in network byte order already */
  212.       out.iam.header=in.who.header;
  213.       out.iam.pid=htons(getpid());
  214.       if(sendto(isock, (char *)&out, sizeof(IAmMessage), 0,
  215.             (struct sockaddr *)&oname, sizeof(oname)) < 0)
  216.         error("sending response to pid inquiry");
  217.       break;
  218.     default: ; /* ignore other messages */
  219.       }
  220.     }
  221.   }
  222. }
  223.  
  224.  
  225. /* This function is called when we get a SIGALRM so that we die gracefully */
  226. /* with exit status 0; otherwise inetd would log a message about us getting */
  227. /* an alarm signal */
  228.  
  229. int die()
  230. { exit(0);
  231. }
  232.  
  233. int main(argc, argv)
  234.      int argc; char **argv;
  235. { int i;
  236.   me=argv[0];
  237.   signal(SIGALRM, die);
  238.   while(--argc)
  239.   { char *s= *++argv;
  240.     if(*s++ != '-')
  241.       goto usage;
  242.     switch(*s)
  243.     { 
  244.     case 'n': /* nice */
  245.       niceval=atoi(++s);
  246.       break;
  247.     case 't': /* timeout */
  248.       timeout=atoi(++s);
  249.       break;
  250.     case 'i': /* accept for backwards compatibility but ignore */
  251.       break;
  252.     default: 
  253.       goto usage;
  254.     }
  255.   }
  256.   nice(niceval);
  257.   serve(); /* never returns */
  258.  usage:
  259.   exit(1);
  260. }
  261.